export const state = () => ({
  language: 'zh',
  // 用户个人信息
  profile: null,
  // 收藏内容
  collections: [],
  // 浏览历史
  history: [],
  // 发布内容
  publications: [],
  // 通知
  notifications: [],
  // 未读通知数量
  unreadNotificationCount: 0,
  // 用户设置
  settings: {
    appearance: {
      darkTheme: false,
      primaryColor: '#1976D2',
      fontSize: 14,
      density: 'comfortable'
    },
    content: {
      categories: [],
      languages: ['zh'],
      displayMode: 'grid'
    },
    privacy: {
      profileVisibility: 'public',
      activityTracking: true,
      showOnlineStatus: true,
      allowContentSharing: true
    },
    notifications: {
      enabled: true,
      types: [
        { type: 'comments', enabled: true },
        { type: 'likes', enabled: true },
        { type: 'follows', enabled: true },
        { type: 'mentions', enabled: true },
        { type: 'system', enabled: true }
      ],
      methods: {
        inApp: true,
        email: true
      },
      emailFrequency: 'daily'
    }
  }
})

export const mutations = {
  SET_LANGUAGE(state, lang) {
    state.language = lang
  },
  // 设置用户个人信息
  SET_PROFILE(state, profile) {
    state.profile = profile
  },
  // 设置收藏内容
  SET_COLLECTIONS(state, collections) {
    state.collections = collections
  },
  // 添加收藏
  ADD_COLLECTION(state, collection) {
    state.collections.unshift(collection)
  },
  // 移除收藏
  REMOVE_COLLECTION(state, id) {
    state.collections = state.collections.filter(item => item.id !== id)
  },
  // 设置浏览历史
  SET_HISTORY(state, history) {
    state.history = history
  },
  // 添加浏览记录
  ADD_HISTORY(state, historyItem) {
    // 检查是否已存在相同ID的记录
    const existingIndex = state.history.findIndex(item => item.id === historyItem.id)
    if (existingIndex !== -1) {
      // 如果存在，则移除旧记录
      state.history.splice(existingIndex, 1)
    }
    // 添加到历史记录开头
    state.history.unshift(historyItem)
  },
  // 清空浏览历史
  CLEAR_HISTORY(state) {
    state.history = []
  },
  // 设置发布内容
  SET_PUBLICATIONS(state, publications) {
    state.publications = publications
  },
  // 添加发布内容
  ADD_PUBLICATION(state, publication) {
    state.publications.unshift(publication)
  },
  // 更新发布内容
  UPDATE_PUBLICATION(state, updatedPublication) {
    const index = state.publications.findIndex(item => item.id === updatedPublication.id)
    if (index !== -1) {
      state.publications.splice(index, 1, updatedPublication)
    }
  },
  // 删除发布内容
  REMOVE_PUBLICATION(state, id) {
    state.publications = state.publications.filter(item => item.id !== id)
  },
  // 设置通知
  SET_NOTIFICATIONS(state, notifications) {
    state.notifications = notifications
    state.unreadNotificationCount = notifications.filter(n => !n.read).length
  },
  // 添加通知
  ADD_NOTIFICATION(state, notification) {
    state.notifications.unshift(notification)
    if (!notification.read) {
      state.unreadNotificationCount++
    }
  },
  // 标记通知为已读
  MARK_NOTIFICATION_READ(state, id) {
    const notification = state.notifications.find(n => n.id === id)
    if (notification && !notification.read) {
      notification.read = true
      state.unreadNotificationCount--
    }
  },
  // 标记所有通知为已读
  MARK_ALL_NOTIFICATIONS_READ(state) {
    state.notifications.forEach(notification => {
      notification.read = true
    })
    state.unreadNotificationCount = 0
  },
  // 删除通知
  REMOVE_NOTIFICATION(state, id) {
    const notification = state.notifications.find(n => n.id === id)
    if (notification && !notification.read) {
      state.unreadNotificationCount--
    }
    state.notifications = state.notifications.filter(n => n.id !== id)
  },
  // 设置用户设置
  SET_SETTINGS(state, settings) {
    state.settings = {
      ...state.settings,
      ...settings
    }
  },
  // 更新外观设置
  UPDATE_APPEARANCE_SETTINGS(state, appearanceSettings) {
    state.settings.appearance = {
      ...state.settings.appearance,
      ...appearanceSettings
    }
  },
  // 更新内容偏好设置
  UPDATE_CONTENT_SETTINGS(state, contentSettings) {
    state.settings.content = {
      ...state.settings.content,
      ...contentSettings
    }
  },
  // 更新隐私设置
  UPDATE_PRIVACY_SETTINGS(state, privacySettings) {
    state.settings.privacy = {
      ...state.settings.privacy,
      ...privacySettings
    }
  },
  // 更新通知设置
  UPDATE_NOTIFICATION_SETTINGS(state, notificationSettings) {
    state.settings.notifications = {
      ...state.settings.notifications,
      ...notificationSettings
    }
  }
}

export const actions = {
  setLanguage({ commit }, lang) {
    commit('SET_LANGUAGE', lang)
    // 注意：在Vuex actions中不能直接使用this.$i18n，应该在组件中处理
  },
  // 获取用户个人信息
  async fetchProfile({ commit }) {
    try {
      const { data } = await this.$axios.get('/user/profile')
      commit('SET_PROFILE', data)
      return data
    } catch (error) {
      console.error('获取用户信息失败', error)
      throw error
    }
  },
  // 获取收藏内容
  async fetchCollections({ commit }) {
    try {
      const { data } = await this.$axios.get('/user/collections')
      commit('SET_COLLECTIONS', data)
      return data
    } catch (error) {
      console.error('获取收藏内容失败', error)
      throw error
    }
  },
  // 添加收藏
  async addCollection({ commit }, contentId) {
    try {
      const { data } = await this.$axios.post('/user/collections', { contentId })
      commit('ADD_COLLECTION', data)
      return data
    } catch (error) {
      console.error('添加收藏失败', error)
      throw error
    }
  },
  // 移除收藏
  async removeCollection({ commit }, id) {
    try {
      await this.$axios.delete(`/user/collections/${id}`)
      commit('REMOVE_COLLECTION', id)
    } catch (error) {
      console.error('移除收藏失败', error)
      throw error
    }
  },
  // 获取浏览历史
  async fetchHistory({ commit }) {
    try {
      const { data } = await this.$axios.get('/user/history')
      commit('SET_HISTORY', data)
      return data
    } catch (error) {
      console.error('获取浏览历史失败', error)
      throw error
    }
  },
  // 添加浏览记录
  async addHistory({ commit }, historyItem) {
    try {
      const { data } = await this.$axios.post('/user/history', historyItem)
      commit('ADD_HISTORY', data)
      return data
    } catch (error) {
      console.error('添加浏览记录失败', error)
      throw error
    }
  },
  // 清空浏览历史
  async clearHistory({ commit }) {
    try {
      await this.$axios.delete('/user/history')
      commit('CLEAR_HISTORY')
    } catch (error) {
      console.error('清空浏览历史失败', error)
      throw error
    }
  },
  // 获取发布内容
  async fetchPublications({ commit }) {
    try {
      const { data } = await this.$axios.get('/user/publications')
      commit('SET_PUBLICATIONS', data)
      return data
    } catch (error) {
      console.error('获取发布内容失败', error)
      throw error
    }
  },
  // 添加发布内容
  async addPublication({ commit }, publication) {
    try {
      const { data } = await this.$axios.post('/user/publications', publication)
      commit('ADD_PUBLICATION', data)
      return data
    } catch (error) {
      console.error('添加发布内容失败', error)
      throw error
    }
  },
  // 更新发布内容
  async updatePublication({ commit }, { id, publication }) {
    try {
      const { data } = await this.$axios.put(`/user/publications/${id}`, publication)
      commit('UPDATE_PUBLICATION', data)
      return data
    } catch (error) {
      console.error('更新发布内容失败', error)
      throw error
    }
  },
  // 删除发布内容
  async removePublication({ commit }, id) {
    try {
      await this.$axios.delete(`/user/publications/${id}`)
      commit('REMOVE_PUBLICATION', id)
    } catch (error) {
      console.error('删除发布内容失败', error)
      throw error
    }
  },
  // 获取通知
  async fetchNotifications({ commit }) {
    try {
      const { data } = await this.$axios.get('/user/notifications')
      commit('SET_NOTIFICATIONS', data)
      return data
    } catch (error) {
      console.error('获取通知失败', error)
      throw error
    }
  },
  // 标记通知为已读
  async markNotificationRead({ commit }, id) {
    try {
      await this.$axios.put(`/user/notifications/${id}/read`)
      commit('MARK_NOTIFICATION_READ', id)
    } catch (error) {
      console.error('标记通知已读失败', error)
      throw error
    }
  },
  // 标记所有通知为已读
  async markAllNotificationsRead({ commit }) {
    try {
      await this.$axios.put('/user/notifications/read-all')
      commit('MARK_ALL_NOTIFICATIONS_READ')
    } catch (error) {
      console.error('标记所有通知已读失败', error)
      throw error
    }
  },
  // 删除通知
  async removeNotification({ commit }, id) {
    try {
      await this.$axios.delete(`/user/notifications/${id}`)
      commit('REMOVE_NOTIFICATION', id)
    } catch (error) {
      console.error('删除通知失败', error)
      throw error
    }
  },
  // 更新用户设置
  async updateSettings({ commit }, settings) {
    try {
      const { data } = await this.$axios.put('/user/settings', settings)
      commit('SET_SETTINGS', data)
      return data
    } catch (error) {
      console.error('更新用户设置失败', error)
      throw error
    }
  }
}

export const getters = {
  // 获取用户语言
  language: state => state.language,
  // 获取用户信息
  profile: state => state.profile,
  // 获取收藏内容
  collections: state => state.collections,
  // 获取浏览历史
  history: state => state.history,
  // 获取发布内容
  publications: state => state.publications,
  // 获取通知
  notifications: state => state.notifications,
  // 获取未读通知数量
  unreadNotificationCount: state => state.unreadNotificationCount,
  // 获取用户设置
  settings: state => state.settings
}